<?php
namespace Home\Controller;
use Think\Controller;
class JiazhangController extends CommonController {
	//家长中心  我的预约
    public function index()
    {
        //公共显示变量
        $this->public_xianshi();

        //我的预约显示
        $sql = "select t1.*,t2.sfz_name,t2.phone from jz_to_ls_yuyue t1 inner join laoshi_user t2 on t1.ls_uid=t2.id where t1.jz_uid='{$_SESSION['id']}' and is_shenhe=0";
        $yuyues = M()->query($sql);
        $this->assign('yuyues',$yuyues);

       $this->display();
    }
    //家长预定单 取消 删除
    public function jz_order_del()
    {
        if (IS_POST) {
            $id = I('post.id');
            $res = M('jz_to_ls_yuyue')->delete($id);
            if ($res) {
                 $arr = array(
                        'error' =>1,
                        'info'=>"取消成功"
                        );
                echo json_encode($arr);
            }else {
                 $arr = array(
                        'error' =>0,
                        'info'=>"取消失败"
                        );
                echo json_encode($arr);
            }
        }
    }

    //家长中心  我的订单
    public function jz_order()
    {
        //公共显示变量
        $this->public_xianshi();

        //我的订单显示
        $sql = "select t1.*,t2.sfz_name,t2.phone from jz_to_ls_yuyue t1 inner join laoshi_user t2 on t1.ls_uid=t2.id where t1.jz_uid='{$_SESSION['id']}' and is_laoshi_on=1 and is_shenhe=1";
        $orders = M()->query($sql);
        $this->assign('orders',$orders);

       $this->display();
    }
    //家长签到评论
    public function qiandaosl()
    {
        if (IS_POST) {
            $id = I('post.id');
            $data['comment'] = I('post.comm');
            $data['jz_qd'] = '1';
            $data['jz_time'] = time();

            if (empty($data['comment'])) {
                 $arr = array(
                        'error' =>0,
                        'info'=>"请您作出评论"
                        );
                echo json_encode($arr);
                exit;
            }

            $res = M('jz_sign')->where(['id'=>$id])->save($data);
            if ($res) {

                //获取课程
                $signs = M('jz_sign')->where(['id'=>$id])->find();
                //检测订单信息
                $kechenginfo = M('jz_to_ls_yuyue')->where(['id'=>$signs['kec_id']])->find();

                //发短信通知老师
                $ls_phone = M('laoshi_user')->where(array('id'=>$kechenginfo['ls_uid']))->getField('phone');
                //家长提交签到 自动通知老师
                $moban = "【老师上门】课程签到通知：".$signs['date']."日课程家长已确认签到。";
                tongz_miaodi($ls_phone,$moban);

                //家长签到完自动减去课时
                $zong_ks = $kechenginfo['zong_ks'];
                $yishang_ks = $kechenginfo['yishang_ks'] + $signs['keshi'];
                $shengyu_ks = $zong_ks - $yishang_ks;
                $keshis = array('shengyu_ks'=>$shengyu_ks,'yishang_ks'=>$yishang_ks);
                M('jz_to_ls_yuyue')->where(['id'=>$signs['kec_id']])->setField($keshis);

                //剩余课时小于等于3 自动发信息到会员手机
                $js_phone = M('jiazhang_user')->where(array('id'=>$kechenginfo['jz_uid']))->getField('phone');//家长手机
                if ($shengyu_ks < 4) {
                    kes_miaodi($js_phone);
                }

                 $arr = array(
                        'error' =>1,
                        'info'=>"签到成功"
                        );
                echo json_encode($arr);
            }else {
                 $arr = array(
                        'error' =>0,
                        'info'=>"签到失败"
                        );
                echo json_encode($arr);
            }
            
        }else {
            $id = I('get.id');
            //所有授课
            $signs = M('jz_sign')->where(['kec_id'=>$id])->order('addtime desc')->select();
            $this->assign('signs',$signs);

            $this->display();
        }
    }

    //家长中心 家长信息设置
    public function jiazhang_one()
    {
        //公共显示变量
        $this->public_xianshi();

        $id = $_SESSION['id'];
        $jiazhang_info = M('jiazhang_user')->where(array('id'=>$id))->find();
        $this->assign('jiazhang_info',$jiazhang_info);

        $cityid = M('city')->where(array('city_name'=>$jiazhang_info['city']))->getField('cityid');

        //显示城市
        $citys = M('city')->where('parent_id=0')->select();
        $this->assign('citys',$citys);

        $this->display();
    }
    //家长中心 家长添加 更新
    public function jiazhang_one_add()
    {   //[city] => 广州[name] => 李先生[sex] => 男
        if (IS_POST) {
            $data['city'] = I('post.city');
            $data['name'] = I('post.name');
            $data['sex'] = I('post.sex');

            $res = M('jiazhang_user')->where(array('id'=>$_SESSION['id']))->save($data);
            if ($res) {
                 $arr = array(
                        'error' =>1,
                        'info'=>"更新成功"
                        );
                echo json_encode($arr);
            }else {
                 $arr = array(
                        'error' =>0,
                        'info'=>"更新失败"
                        );
                echo json_encode($arr);
            }
        }

    }


    //家长中心 家长需求 学生表设置
    public function jiazhang_info()
    {
        //公共显示变量
        $this->public_xianshi();

    	$id = $_SESSION['id'];
    	$city_name = M('jiazhang_user')->where(array('id'=>$id))->getField('city');
        $this->assign('city_name',$city_name);

    	$cityid = M('city')->where(array('city_name'=>$city_name))->getField('cityid');
    	//对应区域
    	$city_quyu = M('city')->where(array('parent_id'=>$cityid))->select();
    	$this->assign('city_quyu',$city_quyu);

        //两大类
        $whs = M('grade')->where(array('parent_id'=>0))->select();
        $this->assign('whs',$whs);
        //获取年级选择的
        $wh = I('get.wh')?I('get.wh'):'文化语言';
        $this->assign('wh',$wh);

        if ($wh == '文化语言') {
            //年级
            $grades = M('grade')->where(array('parent_id'=>1))->select();
            $this->assign('grades',$grades);
            //获取年级选择的
            $grade_name = I('get.grade')?I('get.grade'):'小学';
            $this->assign('grade_name',$grade_name);

            //课程显示 根据年级显示课程
            $gradeid = M('grade')->where(array('grade_name'=>$grade_name))->getField('gradeid');
            $kecheng = M('grade')->where(array('parent_id'=>$gradeid))->select();
            $this->assign('kecheng',$kecheng);
            //获取课程科目选择的
            $subject_name = I('get.subject')?I('get.subject'):'语文';
            $this->assign('subject_name',$subject_name);
        }else {
            //艺术课程 二级类
            $grades = M('grade')->where(array('parent_id'=>2))->select();
            $this->assign('grades',$grades);
            //获取年级选择的
            $grade_name = I('get.grade')?I('get.grade'):'小学';
            $this->assign('grade_name',$grade_name);

            //课程显示 根据年级显示课程
            $gradeid = M('grade')->where(array('grade_name'=>$grade_name))->getField('gradeid');
            $kecheng = M('grade')->where(array('parent_id'=>$gradeid))->select();
            $this->assign('kecheng',$kecheng);
            //获取课程科目选择的
            $subject_name = I('get.subject')?I('get.subject'):'语文';
            $this->assign('subject_name',$subject_name);
        }

        //基本信息显示
        $jiazhang_info = M('jiazhang_xuqiu')->where(array('uid'=>$_SESSION['id']))->find();
        $this->assign('jiazhang_info',$jiazhang_info);


    	$this->display();
    }

    //家长中心 家长需求基本信息
    public function jiazhang_info_add()
    {   //Array ( [sk_city] => 广州 [quyu] => 天河区 [name] => v [sex] => 男 [grade] => 高三 [kecheng] => Array ( [0] => 1 [1] => 2 ) )
    	if (IS_POST) {
    		$data['sk_city'] = I('post.sk_city');
    		$data['sk_quyu'] = I('post.quyu');
    		$data['name'] = I('post.name');
    		$data['sex'] = I('post.sex');

            $data['class'] = I('post.wh');
            $data['grade'] = I('post.grades');
            $data['subjects'] = I('post.subject');
            $data['time'] = time();

    		$data['uid'] = $_SESSION['id'];
    		$uid = M('jiazhang_xuqiu')->where(array('uid'=>$_SESSION['id']))->getField('uid');
    		if ($uid) {
    			$res = M('jiazhang_xuqiu')->where(array('uid'=>$_SESSION['id']))->save($data);
    			if ($res) {
			         $arr = array(
			                'error' =>1,
			                'info'=>"更新成功"
			                );
			        echo json_encode($arr);
    			}else {
			         $arr = array(
			                'error' =>0,
			                'info'=>"更新失败"
			                );
			        echo json_encode($arr);
    			}
    		}else {
    			$res = M('jiazhang_xuqiu')->add($data);
    			if ($res) {
			         $arr = array(
			                'error' =>1,
			                'info'=>"添加成功"
			                );
			        echo json_encode($arr);
    			}else {
			         $arr = array(
			                'error' =>0,
			                'info'=>"添加失败"
			                );
			        echo json_encode($arr);
    			}
    		}
    	}
    }

    //头像
    public function head()
    {
        //公共显示变量
        $this->public_xianshi();


        $id = $_SESSION['id'];
        $head = M('jiazhang_user')->where(array('id'=>$_SESSION['id']))->getField('face');
        $this->assign('head',$head);

        $this->display();
    }
    //添加 修改
    public function head_add()
    {
        if(!empty($_FILES['file']['tmp_name'])){
            $upload = new \Think\Upload();// 实例化上传类    
            $upload->maxSize   =     3145728 ;// 设置附件上传大小    
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
            $upload->savePath  =      'Uploads_jz/'; // 设置附件上传目录    
            // 上传单个文件     
            $info   =   $upload->uploadOne($_FILES['file']);    
            if(!$info) {// 上传错误提示错误信息        
                //$this->error($upload->getError());
            }else{
            // 上传成功 获取上传文件信息         
                $face =  '/Uploads'.'/'.$info['savepath'].$info['savename'];   
                M('jiazhang_user')->where(array('id'=>$_SESSION['id']))->setField('face',$face);
            }
        }
        $this->redirect('jiazhang/head');
    }

    //安全 密码
    public function password()
    {
        //公共显示变量
        $this->public_xianshi();


        $this->display();
    }

    //安全 密码重置
    public function password_add()
    {
        //Array ( [password] => qq [newpassword] => qq [renewpassword] => qq )
        if (IS_POST) {
            $password = I('post.password');
            $newpassword = I('post.newpassword');
            $renewpassword = I('post.renewpassword');

            //验证原始密码
            $password_yz = M('jiazhang_user')->where(array('id'=>$_SESSION['id']))->getField('password');
            if (md5($password) != $password_yz) {
                $arr = array(
                    'error' =>0,
                    'info' =>'原密码输入有误'
                    );
                echo json_encode($arr);
                exit;
            }

            //新密码 验证
            $preg_password = "/^[0-9A-z]{6,16}$/";
            $password_Yz = preg_match($preg_password, $newpassword);
            if (!$password_Yz) {
                $arr = array(
                    'error' => 0,
                    'info' => '密码必须是6-16位的字符串'
                    );
                echo json_encode($arr);
                exit;
            }

            //第二次密码跟第一次检测
            if ($newpassword != $renewpassword) {
                $arr = array(
                    'error' => 0,
                    'info' => '两次密码输入不一样'
                    );
                echo json_encode($arr);
                exit;
            }

            //更新
            $res = M('jiazhang_user')->where(array('id'=>$_SESSION['id']))->setField('password',md5($newpassword));
            if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '更新成功,需要重新登陆'    
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '更新失败'    
                    );
                echo json_encode($arr);
            }
        }
    }



    //无注册家长需求表 提交
    public function zhao_laoshi()
    {
        //公共显示变量
        $this->public_xianshi();

        $post_grade = I('post.grade');
        $post_class = I('post.class');
        //课程
        $grade = I('get.grade')?I('get.grade'):$post_grade;
        $this->assign('grade',$grade);
        //年级类别
        $class = I('get.name')?I('get.name'):$post_class;
        $this->assign('class',$class);

        if ($grade == '艺术兴趣') {
            //年级
            $grades = M('grade')->where(array('parent_id'=>2))->select();
        }else{
            //年级
            $grades = M('grade')->where(array('parent_id'=>1))->select();
        }
        $this->assign('grades',$grades);

        //科目
        $gradeid = M('grade')->where(array('grade_name'=>$class))->getField('gradeid');
        $kemus = M('grade')->where(array('parent_id'=>$gradeid))->select();
        $this->assign('kemus',$kemus);

    	$this->display();
    }
    //
    public function zhao_laoshi_add()
    {//Array ( [name] => aa [kemus] => 流行 [addr] => aaa [phone] => 13430311111 [beizhu] => aa [city] => 广州 [grade] => 艺术兴趣 [class] => 声乐 )
        if (IS_POST) {
             $data['name'] = I('post.name');

             $data['grade'] = I('post.grade');
             $data['class'] = I('post.class');
             $data['beizhu'] = I('post.beizhu');

             $data['kemu'] = I('post.kemus');
             $data['addr'] = I('post.addr');
             $data['phone'] = I('post.phone');

             $data['city'] = I('post.city');
             $data['create_time'] = time();

              //手机
              $preg_js = "/^1[3458][0-9]{9}$/";
              $pass_sj = preg_match($preg_js, $data['phone']);
             if (!$pass_sj) {
                $this->error('请输入正确的手机号');
             }
             //入库
             $res = M('zhao_laoshi')->add($data);
             if ($res) {
                //$this->redirect('index/index',array('newcity'=>$data['city']));
                $this->success('需求提交成功,我们课程顾问会第一时间联系您','Index-index',array('newcity'=>$data['city']));
             }else {
                $this->error('提交失败');
             }
        }
    }

    //无注册家长需求表 添加
    // public function zhao_laoshi_add()
    // {   
    // 	if (IS_POST) {
    // 		$data['name'] = I('post.name');

    //         $data['grade'] = I('post.grade');
    //         $data['beizhu'] = I('post.beizhu');

    // 		$data['kemu'] = I('post.kemu');
    // 		$data['addr'] = I('post.addr');
    // 		$data['phone'] = I('post.phone');

    // 		$data['city'] = I('post.city');
    //         $data['create_time'] = time();

	   //     	 //手机
	   //     	 $preg_js = "/^1[3458][0-9]{9}$/";
	   //     	 $pass_sj = preg_match($preg_js, $data['phone']);
			 // if (!$pass_sj) {
				// $arr = array(
				// 	'error' => 0,
				// 	'info' => '请输入正确的手机号'
				// 	);
				// echo json_encode($arr);
				// exit;
			 // }

			 // //入库
			 // $res = M('zhao_laoshi')->add($data);
			 // if ($res) {
				// $arr = array(
				// 	'error' => 1,
				// 	'info' => '需求提交成功,我们课程顾问会第一时间联系您'
				// 	);
				// echo json_encode($arr);
			 // }else {
				// $arr = array(
				// 	'error' => 0,
				// 	'info' => '需求提交失败,请重新提交'
				// 	);
				// echo json_encode($arr);
			 // }
    // 	}
    // }

}